﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GiaoTrinhEPU.Repositories.Repositories
{
    public class UserRepository
    {
        public IEnumerable<UserProfile> ListUser()
        {
            IEnumerable<UserProfile> user;
            using (var db = new ThuVienEPUDataContext())
            {
              return  user = db.UserProfiles.OrderBy(a => a.Name).ToList();
            }
        }

        public UserProfile GetUserbyUserId(int Id)
        {
            var user = new UserProfile();
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                   user = db.UserProfiles.Single(a => a.UserId.Equals(Id));
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
            return user;
        }

        public void UpdateUser(UserProfile item)
        {
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    var user = db.UserProfiles.Single(a => a.UserId.Equals(item.UserId));
                    user.UserId = item.UserId;
                    user.Name = item.Name;
                    user.Email = item.Email;
                    user.Address = item.Address;
                    user.Mobile = item.Mobile;
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
        }
    }
}
